<template>
  <el-card shadow="hover">
    <!--    查询条件部分-->
    <div>
      <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">供暖计费日报信息展示</span></el-divider>
      <!--      数据操作按钮-->
      <div style="padding: 10px" align="right">
        <el-button-group>
          <el-button type="primary" :loading="downloadLoading" icon="el-icon-plus" @click="handleDownload">导出Excel</el-button>
        </el-button-group>
      </div>

      <el-table
        id="out-table"
        v-loading="loading"
        :data="dataSource"
        :border="true"
        show-summary
        align="center"
        empty-text="暂无数据"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          :index="indexMethod"
          fixed="left"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          label="小区名称"
          fixed="left"
          align="center"
          min-width="80"
          :header-align="columnsOptions.headerAlign"
          prop="villageName"
        />
        <el-table-column
          label="总户数"
          fixed="left"
          align="right"
          min-width="80"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="allHouseholdCount"
        >
          <template slot-scope="{row}">
            {{ row.allHouseholdCount }} 户
          </template>
        </el-table-column>
        <el-table-column
          label="总缴费面积"
          fixed="left"
          min-width="80"
          align="right"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="allHouseholdHeatingAreaCount"
        >
          <template slot-scope="{row}">
            {{ row.allHouseholdHeatingAreaCount }} ㎡
          </template>
        </el-table-column>
        <el-table-column
          label="总缴费金额"
          fixed="left"
          min-width="80"
          align="right"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="allHouseholdPaidAmountCount"
        >
          <template slot-scope="{row}">
            {{ row.allHouseholdPaidAmountCount }} ￥
          </template>
        </el-table-column>
        <el-table-column
          label="今日缴费户数"
          fixed="left"
          align="right"
          min-width="80"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="todayPaidHouseHoldCount"
        >
          <template slot-scope="{row}">
            {{ row.todayPaidHouseHoldCount }} 户
          </template>
        </el-table-column>
        <el-table-column
          label="今日缴费供热面积"
          fixed="left"
          min-width="80"
          align="right"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="todayPaidHeatingAreaCount"
        >
          <template slot-scope="{row}">
            {{ row.todayPaidHeatingAreaCount }} ㎡
          </template>
        </el-table-column>
        <el-table-column
          label="今日缴费金额"
          fixed="left"
          min-width="80"
          align="right"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="todayPaidAmountCount"
        >
          <template slot-scope="{row}">
            {{ row.todayPaidAmountCount }} ￥
          </template>
        </el-table-column>
        <el-table-column
          label="累计缴费户数"
          fixed="left"
          min-width="80"
          align="right"
          sortable
          :header-align="columnsOptions.headerAlign"
          prop="cumulativePaidHouseholdCount"
        >
          <template slot-scope="{row}">
            {{ row.cumulativePaidHouseholdCount }} 户
          </template>
        </el-table-column>
        <el-table-column
          label="累计缴费面积"
          min-width="125"
          sortable
          :header-align="columnsOptions.headerAlign"
          align="right"
          prop="cumulativePaidHeatingAreaCount"
        >
          <template slot-scope="{row}">
            {{ row.cumulativePaidHeatingAreaCount }} ㎡
          </template>
        </el-table-column>
      </el-table>
      <div align="center">
        <div style="display: inline-block;width: 60px;height:60px;line-height: 60px;text-align: center;font-size: 15px">
          <span style="text-align: center;line-height: 60px;width: 60px;height:60px;">共{{ dataTotals }}条</span>
        </div>
        <div style="display: inline-block">
          <el-pagination
            :current-page.sync="queryParma.pageIndex"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryParma.pageSize"
            layout="total, sizes, prev,next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { getHeatingAndPayTodaySatisifyDataPage } from '@/api/ht/zffygl/rbxxgl'
const { getDateTime } = require('@/utils/DateUtil')
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
const { Message } = require('element-ui')
export default {
  name: 'Index',
  data() {
    return {
      loading: false,
      columnsOptions: { // 表单展示
        headerAlign: 'center' // 字段lable 位置
      },
      downloadLoading: false, // 下载中图标提示
      dataSource: [], // 信息列表
      dataTotals: 0, // 查询总条数
      searchloding: false, // 搜索按钮加载
      queryParma: {
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.getHeatingAndPayListHandle() // 获取信息列表
  },
  methods: {
    getHeatingAndPayListHandle() {
      this.getHeatingAndPayList()
    },
    async getHeatingAndPayList() {
      this.loading = true
      const res = await getHeatingAndPayTodaySatisifyDataPage(this.queryParma)
      const data = res.data.main.dataArray
      const resp = res.data.respOutMsgHeader
      const totals = res.data.main.totalsData
      const currentPage = res.data.main.currentPage
      if (resp.respCode === 200) {
        setTimeout(() => {
          this.searchloding = false
          this.loading = false
        }, 500)
        this.dataSource = data
        this.dataTotals = totals
        this.queryParma.pageIndex = currentPage
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.loading = false
      }
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计'
          return
        }
      })
      return sums
    },
    indexMethod(index) {
      return index + 1 + (this.queryParma.pageIndex - 1) * this.queryParma.pageSize
    },
    handleDownload() {
      const fix = document.querySelector('.el-table__fixed')
      let wb
      if (fix) {
        // 判断要导出的节点中是否有fixed的表格，如果有，转换excel时先将该dom移除，然后append回去 可以避免行重复
        wb = XLSX.utils.table_to_book(
          document.querySelector('#out-table').removeChild(fix)
        )
        document.querySelector('#out-table').appendChild(fix)
      } else {
        wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
      }
      const wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        // 文件名可以自定义
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          '采暖费计费日报表格-' + getDateTime() + '.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },
    getNowTime() {
      return getDateTime()
    },
    handleSizeChange(val) {
      this.queryParma.pageSize = val
      this.getHeatingAndPayListHandle()
    },
    handleCurrentChange(val) {
      this.queryParma.pageIndex = val
      this.getHeatingAndPayListHandle()
    }
  }
}
</script>

<style scoped>
.refresh_Icon{
  margin-left: 5px;
  font-size: 20px;
  display: inline-block;
  cursor: pointer;
}
.refresh_Icon :hover{
  color: #20a0ff;
}
</style>
